<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二手易购 - 我的评论</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 全局样式 */
        :root {
            --primary: #1a1a1a;
            --secondary: #333333;
            --accent: #666666;
            --light: #f5f5f5;
            --white: #ffffff;
            --dark: #000000;
            --light-gray: #e0e0e0;
            --medium-gray: #999999;
            --shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            --transition: all 0.2s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f8f9fa 0%, #eef0f2 100%);
            color: var(--dark);
            min-height: 100vh;
        }

        /* 顶部导航栏 */
        .navbar {
            background: var(--white);
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            padding: 12px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .logo {
            display: flex;
            align-items: center;
        }

        .logo h1 {
            font-size: 22px;
            font-weight: 700;
            color: var(--dark);
            margin-left: 8px;
            letter-spacing: -0.5px;
        }

        .logo i {
            font-size: 24px;
            color: var(--accent);
        }

        .nav-links {
            display: flex;
            gap: 20px;
        }

        .nav-link {
            display: flex;
            flex-direction: column;
            align-items: center;
            text-decoration: none;
            color: var(--dark);
            font-size: 13px;
            transition: var(--transition);
            position: relative;
            padding: 4px 8px;
            border-radius: 6px;
        }

        .nav-link i {
            font-size: 18px;
            margin-bottom: 3px;
        }

        .nav-link:hover {
            color: var(--accent);
            background-color: rgba(0, 0, 0, 0.03);
        }

        .nav-link.active {
            background-color: rgba(0, 0, 0, 0.05);
            color: var(--dark);
            font-weight: 600;
        }

        .user-menu {
            display: flex;
            align-items: center;
            cursor: pointer;
            position: relative;
        }

        .user-avatar {
            width: 38px;
            height: 38px;
            border-radius: 50%;
            background: linear-gradient(135deg, #e0e0e0, #f0f0f0);
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            margin-left: 12px;
            box-shadow: 0 2px 6px rgba(0,0,0,0.1);
        }

        .user-avatar i {
            font-size: 18px;
            color: var(--accent);
        }

        /* 内容区域 */
        .container {
            max-width: 1200px;
            margin: 40px auto;
            padding: 0 20px;
        }

        /* 页面标题 */
        .page-header {
            text-align: center;
            margin-bottom: 40px;
            position: relative;
        }

        .page-header h1 {
            font-size: 36px;
            font-weight: 800;
            letter-spacing: -1px;
            margin-bottom: 15px;
            color: var(--dark);
        }

        .page-header p {
            font-size: 18px;
            color: var(--medium-gray);
            max-width: 600px;
            margin: 0 auto;
            line-height: 1.6;
        }

        .page-header::after {
            content: '';
            position: absolute;
            bottom: -20px;
            left: 50%;
            transform: translateX(-50%);
            width: 100px;
            height: 3px;
            background: var(--dark);
            border-radius: 2px;
        }

        /* 评论列表 */
        .comments-container {
            background: var(--white);
            border-radius: 16px;
            box-shadow: var(--shadow);
            overflow: hidden;
        }

        .comment-filters {
            display: flex;
            justify-content: space-between;
            padding: 20px;
            border-bottom: 1px solid var(--light-gray);
            background: var(--white);
        }

        .filter-group {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .filter-btn {
            padding: 8px 20px;
            background-color: var(--white);
            border: 1px solid var(--light-gray);
            border-radius: 25px;
            cursor: pointer;
            transition: var(--transition);
            font-size: 14px;
            font-weight: 500;
        }

        .filter-btn.active {
            background: var(--dark);
            color: var(--white);
            border-color: var(--dark);
        }

        .search-box {
            position: relative;
        }

        .search-box input {
            padding: 10px 15px 10px 40px;
            border: 1px solid var(--light-gray);
            border-radius: 25px;
            font-size: 14px;
            width: 250px;
            transition: var(--transition);
            outline: none;
        }

        .search-box input:focus {
            border-color: var(--accent);
            box-shadow: 0 0 0 3px rgba(102, 102, 102, 0.1);
        }

        .search-box i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--medium-gray);
        }

        .comments-list {
            padding: 0;
        }

        .comment-item {
            padding: 25px;
            border-bottom: 1px solid var(--light-gray);
            transition: var(--transition);
        }

        .comment-item:hover {
            background-color: rgba(0, 0, 0, 0.02);
        }

        .comment-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            align-items: flex-start;
        }

        .comment-product {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .product-image {
            width: 80px;
            height: 80px;
            border-radius: 8px;
            overflow: hidden;
            background: linear-gradient(135deg, #f0f0f0, #e0e0e0);
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .product-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .product-info {
            display: flex;
            flex-direction: column;
        }

        .product-name {
            font-size: 18px;
            font-weight: 700;
            color: var(--dark);
            margin-bottom: 5px;
            text-decoration: none;
            transition: var(--transition);
        }

        .product-name:hover {
            color: var(--accent);
        }

        .product-price {
            font-size: 16px;
            font-weight: 600;
            color: var(--dark);
        }

        .product-description {
            font-size: 14px;
            color: var(--medium-gray);
            margin-top: 5px;
            max-width: 600px;
        }

        .product-category {
            display: inline-block;
            padding: 3px 8px;
            background: var(--light-gray);
            border-radius: 4px;
            font-size: 12px;
            color: var(--accent);
            margin-top: 8px;
        }

        .comment-date {
            font-size: 14px;
            color: var(--medium-gray);
        }

        .comment-content {
            padding: 15px 0;
            font-size: 16px;
            line-height: 1.7;
            color: var(--secondary);
            border-left: 3px solid var(--dark);
            padding-left: 20px;
            margin: 15px 0;
            background: rgba(0, 0, 0, 0.02);
            border-radius: 0 8px 8px 0;
        }

        .comment-actions {
            display: flex;
            gap: 15px;
            justify-content: flex-end;
        }

        .action-btn {
            padding: 8px 20px;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
            gap: 8px;
            border: none;
        }

        .edit-btn {
            background: var(--white);
            border: 1px solid var(--dark);
            color: var(--dark);
        }

        .edit-btn:hover {
            background: var(--dark);
            color: var(--white);
        }

        .delete-btn {
            background: var(--white);
            border: 1px solid #dc3545;
            color: #dc3545;
        }

        .delete-btn:hover {
            background: #dc3545;
            color: var(--white);
        }

        /* 空状态 */
        .empty-state {
            text-align: center;
            padding: 70px 20px;
            color: var(--medium-gray);
        }

        .empty-state i {
            font-size: 60px;
            margin-bottom: 20px;
            opacity: 0.2;
        }

        .empty-state h3 {
            font-size: 24px;
            margin-bottom: 15px;
            color: var(--secondary);
            font-weight: 700;
        }

        .empty-state p {
            font-size: 16px;
            max-width: 450px;
            margin: 0 auto;
            line-height: 1.6;
        }

        /* 分页 */
        .pagination {
            display: flex;
            justify-content: center;
            padding: 25px 0;
            gap: 10px;
        }

        .page-btn {
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 8px;
            background: var(--white);
            border: 1px solid var(--light-gray);
            color: var(--dark);
            cursor: pointer;
            transition: var(--transition);
            font-size: 14px;
            font-weight: 600;
        }

        .page-btn:hover, .page-btn.active {
            background: var(--dark);
            color: var(--white);
            border-color: var(--dark);
        }

        .page-btn.disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }

        /* 模态框 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            z-index: 2000;
            overflow-y: auto;
            padding: 15px;
        }

        .modal-content {
            background: var(--white);
            border-radius: 16px;
            max-width: 600px;
            margin: 50px auto;
            position: relative;
            box-shadow: 0 20px 45px rgba(0,0,0,0.2);
        }

        .modal-header {
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid var(--light-gray);
        }

        .modal-title {
            font-size: 22px;
            font-weight: 700;
            letter-spacing: -0.5px;
        }

        .close-modal {
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
            color: var(--medium-gray);
            transition: var(--transition);
            width: 36px;
            height: 36px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
        }

        .close-modal:hover {
            color: var(--dark);
            background: rgba(0,0,0,0.05);
        }

        .modal-body {
            padding: 25px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: var(--dark);
        }

        .form-control {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid var(--light-gray);
            border-radius: 10px;
            font-size: 16px;
            transition: var(--transition);
            background-color: var(--white);
            color: var(--dark);
            outline: none;
            min-height: 150px;
            resize: vertical;
        }

        .form-control:focus {
            border-color: var(--dark);
            box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.05);
        }

        .modal-footer {
            padding: 20px;
            display: flex;
            justify-content: flex-end;
            gap: 15px;
            border-top: 1px solid var(--light-gray);
        }

        .btn {
            padding: 12px 25px;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            border: none;
            position: relative;
            overflow: hidden;
        }

        .btn-outline {
            background: transparent;
            border: 2px solid var(--dark);
            color: var(--dark);
        }

        .btn-outline:hover {
            background-color: var(--dark);
            color: var(--white);
        }

        .btn-dark {
            background: var(--dark);
            color: var(--white);
        }

        .btn-dark:hover {
            background: #111;
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .navbar {
                flex-wrap: wrap;
                padding: 12px 15px;
            }

            .comment-header {
                flex-direction: column;
                gap: 15px;
            }

            .comment-actions {
                justify-content: flex-start;
            }
        }

        @media (max-width: 768px) {
            .comment-filters {
                flex-direction: column;
                gap: 15px;
            }

            .filter-group {
                flex-wrap: wrap;
            }

            .search-box input {
                width: 100%;
            }

            .comment-product {
                flex-direction: column;
                align-items: flex-start;
            }

            .product-info {
                margin-top: 10px;
            }
        }

        /* 用户菜单激活状态 */
        .user-menu.active .dropdown-menu {
            display: block;
        }

        .user-menu.active .user-avatar {
            background: linear-gradient(135deg, #d0d0d0, #e0e0e0);
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<nav class="navbar">
    <div class="logo">
        <i class="fas fa-recycle"></i>
        <h1>二手易购</h1>
    </div>

    <div class="nav-links">
        <a href="productPage.html" class="nav-link">
            <i class="fas fa-home"></i>
            <span>首页</span>
        </a>
        <a href="my-goods.html" class="nav-link">
            <i class="fas fa-box"></i>
            <span>我的商品</span>
        </a>
        <a href="orders.html" class="nav-link">
            <i class="fas fa-file-invoice"></i>
            <span>订单</span>
        </a>
        <a href="my-comments.html" class="nav-link active">
            <i class="fas fa-comment-alt"></i>
            <span>评论</span>
        </a>
        <a href="cart.html" class="nav-link">
            <i class="fas fa-shopping-cart"></i>
            <span>购物车</span>
        </a>
    </div>

    <div class="user-menu" id="userMenu">
        <span id="usernameDisplay">用户</span>
        <div class="user-avatar">
            <i class="fas fa-user"></i>
        </div>
        <div class="dropdown-menu" id="dropdownMenu">
            <a href="profile.html" class="dropdown-item">
                <i class="fas fa-user-circle"></i>个人中心
            </a>
            <div class="divider"></div>
            <a href="#" class="dropdown-item" id="logoutLink">
                <i class="fas fa-sign-out-alt"></i>退出登录
            </a>
        </div>
    </div>
</nav>

<!-- 主内容区域 -->
<div class="container">
    <!-- 页面标题 -->
    <div class="page-header">
        <h1>我的评论</h1>
        <p>查看和管理您在所有商品下的评论</p>
    </div>

    <!-- 评论列表 -->
    <div class="comments-container">
        <!-- 评论筛选 -->
        <div class="comment-filters">
            <div class="filter-group">
                <button class="filter-btn active">全部评论</button>
            </div>
            <div class="search-box">
                <i class="fas fa-search"></i>
                <input type="text" id="searchInput" placeholder="搜索评论内容...">
            </div>
        </div>

        <!-- 评论列表 -->
        <div class="comments-list" id="commentsList">
            <!-- 评论项将通过JS动态加载 -->
        </div>

        <!-- 分页 -->
        <div class="pagination" id="pagination">
            <button class="page-btn" id="prevPageBtn">
                <i class="fas fa-chevron-left"></i>
            </button>
            <!-- 页码将通过JS动态加载 -->
            <button class="page-btn" id="nextPageBtn">
                <i class="fas fa-chevron-right"></i>
            </button>
        </div>
    </div>
</div>

<!-- 编辑评论模态框 -->
<div class="modal" id="editCommentModal">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">编辑评论</h3>
            <button class="close-modal">&times;</button>
        </div>
        <div class="modal-body">
            <div class="form-group">
                <label class="form-label">评论内容</label>
                <textarea class="form-control" id="editCommentText" placeholder="请输入您的评论..."></textarea>
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn btn-outline" id="cancelEditBtn">取消</button>
            <button class="btn btn-dark" id="saveEditBtn">保存更改</button>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal" id="deleteCommentModal">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">删除评论</h3>
            <button class="close-modal">&times;</button>
        </div>
        <div class="modal-body">
            <p style="font-size: 18px; text-align: center; margin: 20px 0;">您确定要删除这条评论吗？此操作不可撤销。</p>
        </div>
        <div class="modal-footer">
            <button class="btn btn-outline" id="cancelDeleteBtn">取消</button>
            <button class="btn btn-dark" style="background: #dc3545; border-color: #dc3545;" id="confirmDeleteBtn">确认删除</button>
        </div>
    </div>
</div>

<script>
    // API 基础URL
    const API_BASE_URL = 'http://localhost:8080';

    // 当前用户信息
    let userInfo = JSON.parse(localStorage.getItem('userInfo')) || null;

    // 当前选中的评论ID
    let currentCommentId = null;
    // 当前页码
    let currentPage = 1;
    // 每页显示数量
    const commentsPerPage = 5;
    // 当前搜索关键词
    let searchKeyword = '';
    // 总页数
    let totalPages = 1;

    // DOM元素
    const commentsList = document.getElementById('commentsList');
    const pagination = document.getElementById('pagination');
    const searchInput = document.getElementById('searchInput');
    const prevPageBtn = document.getElementById('prevPageBtn');
    const nextPageBtn = document.getElementById('nextPageBtn');
    const editCommentModal = document.getElementById('editCommentModal');
    const deleteCommentModal = document.getElementById('deleteCommentModal');
    const editCommentText = document.getElementById('editCommentText');
    const saveEditBtn = document.getElementById('saveEditBtn');
    const cancelEditBtn = document.getElementById('cancelEditBtn');
    const confirmDeleteBtn = document.getElementById('confirmDeleteBtn');
    const cancelDeleteBtn = document.getElementById('cancelDeleteBtn');
    const closeModalButtons = document.querySelectorAll('.close-modal');
    const usernameDisplay = document.getElementById('usernameDisplay');
    const userMenu = document.getElementById('userMenu');
    const dropdownMenu = document.getElementById('dropdownMenu');
    const logoutLink = document.getElementById('logoutLink');

    // 初始化页面
    document.addEventListener('DOMContentLoaded', function() {
        // 检查用户是否登录
        if (!userInfo) {
            alert('请先登录');
            window.location.href = 'login.html';
            return;
        }

        // 更新用户名显示
        usernameDisplay.textContent = userInfo.username || '用户';

        // 渲染评论列表
        fetchComments();

        // 设置事件监听器
        setupEventListeners();
    });

    // 更新用户信息显示
    function updateUserInfo() {
        if (userInfo) {
            document.getElementById('usernameDisplay').textContent = userInfo.username || '用户';
        } else {
            // 如果用户未登录，跳转到登录页
            window.location.href = 'login.html';
        }
    }

    // 设置事件监听器
    function setupEventListeners() {
        // 用户菜单点击事件
        userMenu.addEventListener('click', function(e) {
            e.stopPropagation();
            userMenu.classList.toggle('active');
        });

        // 点击页面其他地方关闭菜单
        document.addEventListener('click', function(e) {
            if (!userMenu.contains(e.target)) {
                userMenu.classList.remove('active');
            }
        });

        // 搜索框事件
        searchInput.addEventListener('input', function() {
            searchKeyword = this.value.trim();
            currentPage = 1;
            fetchComments();
        });

        // 分页按钮事件
        prevPageBtn.addEventListener('click', goToPrevPage);
        nextPageBtn.addEventListener('click', goToNextPage);

        // 模态框关闭按钮
        closeModalButtons.forEach(button => {
            button.addEventListener('click', function() {
                editCommentModal.style.display = 'none';
                deleteCommentModal.style.display = 'none';
            });
        });

        // 编辑评论相关事件
        saveEditBtn.addEventListener('click', saveComment);
        cancelEditBtn.addEventListener('click', function() {
            editCommentModal.style.display = 'none';
        });

        // 删除评论相关事件
        confirmDeleteBtn.addEventListener('click', deleteComment);
        cancelDeleteBtn.addEventListener('click', function() {
            deleteCommentModal.style.display = 'none';
        });

        // 点击模态框外部关闭
        window.addEventListener('click', function(event) {
            if (event.target === editCommentModal) {
                editCommentModal.style.display = 'none';
            }
            if (event.target === deleteCommentModal) {
                deleteCommentModal.style.display = 'none';
            }
        });

        // 退出登录
        logoutLink.addEventListener('click', function(e) {
            e.preventDefault();
            localStorage.removeItem('userInfo');
            window.location.href = 'login.html';
        });
    }

    // 获取评论数据
    function fetchComments() {
        const userId = userInfo.userId;
        const url = `${API_BASE_URL}/comments/user/${userId}/page?page=${currentPage}&size=${commentsPerPage}&keyword=${encodeURIComponent(searchKeyword)}`;

        fetch(url)
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络响应异常');
                }
                return response.json();
            })
            .then(data => {
                // 处理分页数据
                totalPages = data.pages;
                renderComments(data.records);
                renderPagination(data);
            })
            .catch(error => {
                console.error('获取评论失败:', error);
                showEmptyState('获取评论失败，请稍后重试');
            });
    }

    // 渲染评论列表
    function renderComments(comments) {
        // 清空评论列表
        commentsList.innerHTML = '';

        if (!comments || comments.length === 0) {
            showEmptyState('没有找到符合条件的评论');
            return;
        }

        // 渲染评论项
        comments.forEach(comment => {
            const commentItem = document.createElement('div');
            commentItem.className = 'comment-item';

            // 使用DTO字段映射
            commentItem.innerHTML = `
                <div class="comment-header">
                    <div class="comment-product">
                        <div class="product-image">
                            <img src="https://via.placeholder.com/300x300?text=No+Image"
                                 alt="${comment.productName || '商品图片'}"
                                 onerror="this.src='https://via.placeholder.com/300x300?text=Image+Error'">
                        </div>
                        <div class="product-info">
                            <a href="product-detail.html?id=${comment.cGoodsId}" class="product-name">
                                ${comment.productName || '未命名商品'}
                            </a>
                            <div class="product-price">
                                ¥${comment.productPrice ? formatPrice(comment.productPrice) : '0.00'}
                            </div>
                            <div class="product-description">
                                ${comment.description || '暂无商品描述'}
                            </div>
                        </div>
                    </div>
                </div>
                <div class="comment-content">
                    ${comment.cBio || '暂无评论内容'}
                </div>
                <div class="comment-actions">
                    <button class="action-btn edit-btn" data-id="${comment.cId}">
                        <i class="fas fa-edit"></i> 编辑
                    </button>
                    <button class="action-btn delete-btn" data-id="${comment.cId}">
                        <i class="fas fa-trash-alt"></i> 删除
                    </button>
                </div>
            `;

            commentsList.appendChild(commentItem);
        });

        // 重新绑定编辑和删除按钮事件
        bindEditDeleteEvents();
    }

    // 格式化价格
    function formatPrice(price) {
        return parseFloat(price).toFixed(2);
    }

    // 显示空状态
    function showEmptyState(message) {
        commentsList.innerHTML = `
            <div class="empty-state">
                <i class="fas fa-comment-slash"></i>
                <h3>暂无评论</h3>
                <p>${message}</p>
            </div>
        `;

        // 清空分页控件
        pagination.innerHTML = '';
    }

    // 绑定编辑和删除按钮事件
    function bindEditDeleteEvents() {
        // 编辑按钮
        document.querySelectorAll('.edit-btn').forEach(button => {
            button.addEventListener('click', function() {
                currentCommentId = parseInt(this.getAttribute('data-id'));
                const commentContent = this.closest('.comment-item').querySelector('.comment-content').textContent;

                editCommentText.value = commentContent;
                editCommentModal.style.display = 'block';
            });
        });

        // 删除按钮
        document.querySelectorAll('.delete-btn').forEach(button => {
            button.addEventListener('click', function() {
                currentCommentId = parseInt(this.getAttribute('data-id'));
                deleteCommentModal.style.display = 'block';
            });
        });
    }

    // 渲染分页控件
    function renderPagination(pageData) {
        // 清空分页控件（保留上一页和下一页按钮）
        pagination.innerHTML = '';
        pagination.appendChild(prevPageBtn);

        // 计算显示的页码范围
        const startPage = Math.max(1, currentPage - 2);
        const endPage = Math.min(totalPages, startPage + 4);

        // 添加页码按钮
        for (let i = startPage; i <= endPage; i++) {
            const pageBtn = document.createElement('button');
            pageBtn.className = 'page-btn';
            if (i === currentPage) {
                pageBtn.classList.add('active');
            }
            pageBtn.textContent = i;
            pageBtn.addEventListener('click', () => {
                currentPage = i;
                fetchComments();
            });
            pagination.appendChild(pageBtn);
        }

        // 添加下一页按钮
        pagination.appendChild(nextPageBtn);

        // 更新按钮状态
        prevPageBtn.disabled = currentPage === 1;
        nextPageBtn.disabled = currentPage === totalPages;
    }

    // 上一页
    function goToPrevPage() {
        if (currentPage > 1) {
            currentPage--;
            fetchComments();
        }
    }

    // 下一页
    function goToNextPage() {
        if (currentPage < totalPages) {
            currentPage++;
            fetchComments();
        }
    }

    // 保存评论
    function saveComment() {
        if (!currentCommentId) return;

        const newComment = editCommentText.value.trim();
        if (!newComment) {
            alert('评论内容不能为空');
            return;
        }

        // 准备请求数据
        const requestData = {
            cId: currentCommentId,
            cBio: newComment
        };

        // 调用后端API更新评论
        fetch(`${API_BASE_URL}/comments/update`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(requestData)
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('更新评论失败');
                }
                return response.text();
            })
            .then(message => {
                // 关闭模态框
                editCommentModal.style.display = 'none';
                // 刷新评论列表
                fetchComments();
                alert('评论更新成功');
            })
            .catch(error => {
                console.error('更新评论失败:', error);
                alert('更新评论失败: ' + error.message);
            });
    }

    // 删除评论
    function deleteComment() {
        if (!currentCommentId) return;

        // 调用后端API删除评论
        fetch(`${API_BASE_URL}/comments/delete/${currentCommentId}`, {
            method: 'DELETE'
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('删除评论失败');
                }
                return response.text();
            })
            .then(message => {
                // 关闭模态框
                deleteCommentModal.style.display = 'none';
                // 刷新评论列表
                fetchComments();
                alert('评论已删除');
            })
            .catch(error => {
                console.error('删除评论失败:', error);
                alert('删除评论失败: ' + error.message);
            });
    }
</script>
</body>
</html>